package com.wnhz.work.yz.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.bean.ArrangeProgressBean;

import java.util.ArrayList;
import java.util.List;

/**
 * 产业园接待行程
 */
public class ReceptionTravelActivity extends BaseActivity implements View.OnClickListener {

    //    private RecyclerView recyclerView;
//    private View footView;
//    private BaseRVAdapter<ReceptionTravelBean> adapter;
    private SwipeMenuListView swipe;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reception_travel);
        initData();
        initTitle();
        initView();
    }

    private List<ArrangeProgressBean.TripListEntity> trip_list = new ArrayList<>();

    private void initData() {
        trip_list = ArrangeRouteActivity.maxBean.getTrip_list();
    }

    private int dp2px(int dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());
    }

    private MyAdapter adapter;

    private void initView() {
//        footView = LayoutInflater.from(this).inflate(R.layout.includr_visitor_footview, null);
//        footView.findViewById(R.id.add_visitor_lay).setOnClickListener(this);
//
//        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
//        recyclerView.setLayoutManager(new LinearLayoutManager(this));
//        recyclerView.addItemDecoration(new RecycleViewDivider(this, LinearLayout.HORIZONTAL
//                , getResources().getDimensionPixelSize(R.dimen.line_h), getResources().getColor(R.color.line_color)));
//
//        for (int i = 0; i < 4; i++) {
//            list.add(new ReceptionTravelBean());
//        }
//        adapter = new BaseRVAdapter<ReceptionTravelBean>(this, list) {
//            @Override
//            public int getLayoutId(int viewType) {
//                return R.layout.item_receptiontrave;
//            }
//
//            @Override
//            public void onBind(BaseViewHolder holder, int position) {
//
//                holder.setTextView(R.id.time_tv, getResources().getString(R.string.reception_travel_item_time_tv));
//                holder.setTextView(R.id.context_tv, getResources().getString(R.string.reception_travel_item_context_tv));
//            }
//
//        };
//        adapter.addFooterView(footView);
//        recyclerView.setAdapter(adapter);
        adapter = new MyAdapter(trip_list);
        swipe = (SwipeMenuListView) findViewById(R.id.swipe);
        SwipeMenuCreator creater = new SwipeMenuCreator() {
            @Override
            public void create(SwipeMenu menu) {


                //同理create删除item
                SwipeMenuItem deleteItem = new SwipeMenuItem(ReceptionTravelActivity.this);
                // set item background
                deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9, 0x3F, 0x25)));
                // set item width
                deleteItem.setWidth(dp2px(90));
                // set item title
                deleteItem.setTitle("删除");
                // set item title fontsize
                deleteItem.setTitleSize(18);
                // set item title font color
                deleteItem.setTitleColor(Color.WHITE);
//                // set a icon
//                deleteItem.setIcon(R.mipmap.ic_delete);
                // add to menu
                menu.addMenuItem(deleteItem);
            }
        };

        swipe.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(final int position, SwipeMenu menu, int index) {
                switch (index) {

                    case 0:
                        //删除的逻辑
                        showDialogv7("确定删除该行程吗？", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // updelete(position);
                                trip_list.remove(position);
                                adapter.notifyDataSetChanged();

                            }
                        });
                        break;

                    default:
                        break;
                }
                return false;
            }
        });

        swipe.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                startActivityForResult(new Intent(ReceptionTravelActivity.this, AddReceptionTravelActivity.class).putExtra("position", position), 333);
            }
        });
        swipe.setAdapter(adapter);
        if (ArrangeRouteActivity.maxBean.getCanUpdata().equals("0")) {
            findViewById(R.id.add_visitor_lay).setVisibility(View.GONE);

        } else {
            findViewById(R.id.add_visitor_lay).setVisibility(View.VISIBLE);
            findViewById(R.id.add_visitor_lay).setOnClickListener(this);
            swipe.setMenuCreator(creater);
        }
    }

    class MyAdapter extends BaseAdapter {
        List<ArrangeProgressBean.TripListEntity> trip_list = new ArrayList<>();

        public MyAdapter(List<ArrangeProgressBean.TripListEntity> trip_list) {
            this.trip_list = trip_list;
        }

        @Override
        public int getCount() {
            return trip_list.size();
        }

        @Override
        public Object getItem(int position) {
            return trip_list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            MyHolder holder;
            if (convertView == null) {
                holder = new MyHolder();
                convertView = View.inflate(ReceptionTravelActivity.this, R.layout.item_receptiontrave, null);
                holder.name = (TextView) convertView.findViewById(R.id.context_tv);
                holder.time = (TextView) convertView.findViewById(R.id.time_tv);
                convertView.setTag(holder);
            } else {
                holder = (MyHolder) convertView.getTag();
            }
            holder.time.setText(trip_list.get(position).getStart_time());
            holder.name.setText(trip_list.get(position).getPlace_name());
            return convertView;
        }

        class MyHolder {
            TextView name;
            TextView time;
        }
    }

    private TextView title;

    private void initTitle() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText(R.string.reception_travel_title);
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        findViewById(R.id.submit).setOnClickListener(this);
        TextView title_right_tv_save = (TextView) findViewById(R.id.submit);
        title_right_tv_save.setVisibility(View.VISIBLE);
        title_right_tv_save.setText("保存");

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;

            //保存
            case R.id.submit:

                setResult(RESULT_OK);
                finish();
                break;

            //新增行程
            case R.id.add_visitor_lay:
                startActivityForResult(new Intent(this, AddReceptionTravelActivity.class), 333);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 333:
                    adapter.notifyDataSetChanged();
                    break;
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
